package com.VolunteerPlatform.Controller.loadfile;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by DoveXL on 2018/12/10.
 */

@Controller
@RequestMapping(value = "/UploadFile/**")
public class UploadController {
    @RequestMapping("/TeamInfoUpload")
    @ResponseBody
    public String upload(HttpServletRequest request, HttpServletResponse response,
                       @RequestParam("file") CommonsMultipartFile file) throws IOException {
        boolean flag = false;
        if (file.getSize() > 0) {
            String path = "/files/upload/";
            String uploadPath = request.getSession().getServletContext().getRealPath(path);
            System.out.println(uploadPath);
            try {
                FileUtils.copyInputStreamToFile(file.getInputStream(),
                        new File(uploadPath, file.getOriginalFilename()));
                flag = true;
                return "UploadCompleted";
            } catch (Exception e) {
                e.printStackTrace();
                return "UploadException";
            }
        }
        return "UploadOtherException";
    }

}
